#!/usr/bin/perl -w

##HACKABOT_HELP##
# Do you feel lucky?
# !google search phrase
##HACKABOT_HELP##

use strict;
use URI::Escape;
use Hackabot::Client;

my $hbc = Hackabot::Client->new;
my $search = $hbc->readline;
my $asker = $hbc->sent_by;

$search = uri_escape($search);
my $google = `lynx --head --dump "http://www.google.com/search?q=$search\&btnI=I%27m+Feeling+Lucky"`;
my $url;
foreach(split(/\n/,$google)) {
	if (/^Location:\s*(.+)/) {
		$url = $1;
	}
}
if (defined $url) {
	print "send $asker: Google says $url\n";
}
else {
	print "send $asker: Google didn't say much :-/\n";
}
